package com.icangya.DAO.impl;

import com.icangya.DAO.IFbTurntableRecordDAO;
import com.icangya.Utils.SQLUtilt;
import com.icangya.domain.FbTurntableRecord;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.icangya.Value.StaticValue.PHONE_PAGE_ROW;


@Repository
public class FbTurntableRecordDAOImpl extends  BaseDAO<FbTurntableRecord> implements IFbTurntableRecordDAO {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public List<FbTurntableRecord> getTurntableRecordByOpenId(String openId) {

        Session session=  getHibernateTemplate().getSessionFactory().getCurrentSession();
        String hql="FROM FbTurntableRecord mr where  mr.openId ='"+openId+"' order by mr.createTime desc ";
        Query query=session.createQuery(hql);
        query.setMaxResults(10);
        List<FbTurntableRecord> list=query.list();
        if(list.size()>0)
            return list;
        return null;
//        List<FbTurntableRecord> list= (List<FbTurntableRecord>) getHibernateTemplate().find(hql,openId);
//        if(list.size()>0)
//            return list;
//        return null;
    }

    @Override
    public List getTurntableRecordByOpenId(String openId, int startPage) {
        String  hql=" FROM FbTurntableRecord bm where bm.openId='"+openId+"'";
        String count="select count(bm) "+hql;
        Long total = (Long) getHibernateTemplate().find(count).get(0);
        return SQLUtilt.getIstance().doPage(getHibernateTemplate(), hql, total, startPage, PHONE_PAGE_ROW);

    }
}
